<?php

namespace app\common\service;

use app\common\controller\Send;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;

class Email
{

    use Send;

    public static function send($toEmail, $attachment)
    {
        $mail = new PHPMailer(true);
        try {
            // Enable verbose debug output SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
            $mail->SMTPDebug = 0;
            // Set mailer to use SMTP
            $mail->isSMTP();
            // Specify main and backup SMTP servers
            $mail->Host = 'smtp.exmail.qq.com';
            // Enable SMTP authentication
            $mail->SMTPAuth = true;
            // SMTP username
            $mail->Username = 'hailong@boxueteach.com';
            // SMTP password
            $mail->Password = 'ALhu303568';
            // Enable TLS encryption, `ssl` also accepted
            $mail->SMTPSecure = 'ssl';
            // TCP port to connect to
            $mail->Port = 465;

            // Recipients
            $mail->setFrom('hailong@boxueteach.com', '广州博学教育');
            // Add a recipient 接收方
            $mail->addAddress($toEmail);
            // 回复人
            $mail->addReplyTo('hailong@boxueteach.com', '广州博学教育');

            // Attachments
            $mail->addAttachment($attachment);    // Optional name

            // Content
            // Set email format to HTML
            $mail->isHTML(true);
            // 主题
            $mail->Subject = '广州博学教育课件下载';
            // 内容
            $mail->Body = '尊敬的用户，感谢您对广州博学教育的支持，以下是相关课件，请注意查收。';
            // 纯文本消息体。for non-HTML mail clients
            $mail->AltBody = '尊敬的用户，感谢您对广州博学教育的支持，以下是相关课件，请注意查收。';

            $mail->send();

            return self::sendResult(self::$okCode);
        } catch (Exception $e) {
            return self::sendResult(140, [], $mail->ErrorInfo);
        }
    }
}